<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="ES6初学习"><meta name="keywords" content="es6"><meta name="author" content="晟松"><meta name="copyright" content="晟松"><title>ES6初学习 | 晟松</title><link rel="shortcut icon" href="/logo.png"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?c34d81d53341febe06a155941e2df16d";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '5.4.0'
} </script><meta name="generator" content="Hexo 5.4.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0"><span class="toc-number">1.</span> <span class="toc-text">1.箭头函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-let-%E4%B8%8E-const"><span class="toc-number">2.</span> <span class="toc-text">2.let 与 const</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E8%A7%A3%E6%9E%84%E8%B5%8B%E5%80%BC"><span class="toc-number">3.</span> <span class="toc-text">3.解构赋值</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-%E5%B1%95%E5%BC%80%E7%AC%A6-%E4%B8%89%E7%82%B9%E8%BF%90%E7%AE%97%E7%AC%A6-%E2%80%A6-rest%E5%8F%82%E6%95%B0"><span class="toc-number">4.</span> <span class="toc-text">4.展开符&#x2F;三点运算符(…)&#x2F;rest参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-async-%E5%87%BD%E6%95%B0"><span class="toc-number">5.</span> <span class="toc-text">5.async 函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-class%E7%B1%BB"><span class="toc-number">6.</span> <span class="toc-text">6.class类</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E6%A8%A1%E6%9D%BF%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">7.</span> <span class="toc-text">1.模板字符串</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E5%B0%BE%E8%B0%83%E7%94%A8%EF%BC%8C%E5%B0%BE%E9%80%92%E5%BD%92"><span class="toc-number">8.</span> <span class="toc-text">2.尾调用，尾递归</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="/img/avatar.png"></div><div class="author-info__name text-center">晟松</div><div class="author-info__description text-center">晟松的博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://gitee.com/cheng_song">Gitee</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">52</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">45</span></a></div></div></div><div id="content-outer"><div id="top-container" style="background-image: url(/img/blog-bg.jpg)"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">晟松</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">Home</a><a class="site-page" href="/archives">Archives</a><a class="site-page" href="/tags">Tags</a><a class="site-page" href="/categories">Categories</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">ES6初学习</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-07-24</time><div class="post-meta-wordcount"><span>字数总计: </span><span class="word-count">1.2k</span><span class="post-meta__separator">|</span><span>阅读时长: 4 分钟</span></div></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>js的一种版本标准，只是为了学一些重点的新语法。进行一点记录，更细致的还是查看教程吧</p>
<p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/">阮一峰的es6教程</a></p>
<p><a target="_blank" rel="noopener" href="https://juejin.im/post/6844903959283367950">掘金的总结</a></p>
<h4 id="1-箭头函数"><a href="#1-箭头函数" class="headerlink" title="1.箭头函数"></a>1.箭头函数</h4><p><strong>箭头函数的作用主要是定义匿名函数</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> foo = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;&#125;;</span><br><span class="line"><span class="comment">// 转换成箭头函数</span></span><br><span class="line"><span class="keyword">let</span> foo = <span class="function">() =&gt;</span> &#123;&#125;;</span><br></pre></td></tr></table></figure>

<p>基本语法<strong>（参数）</strong>：</p>
<ul>
<li>匿名函数没有参数：() 不能省略，占位作用。<code>let foo = () =&gt; &#123;&#125;;</code></li>
<li>只有一个参数：() 可以省略，也可以不省略。<code>let foo = a =&gt; &#123;&#125;;</code></li>
<li>多个参数，() 不能省略。<code>let foo = (a,b) =&gt; &#123;&#125;;</code></li>
</ul>
<p><strong>箭头函数的特点：</strong></p>
<p>1、不能做构造函数（不能实例化）</p>
<p>2、没有 arguments。 arguments就是函数带的实参，类似linux的argv，可用arguments获取调用的参数。</p>
<p><strong>3、箭头函数没有自己的this，箭头函数的this不是调用的时候决定的，this指向上下文环境。（意思：箭头函数的外层的是否有函数，如果有，箭头函数的this就是外层函数的this，如果没有，则为 window）。是由其定义的时候的上下文决定的，是固定的</strong></p>
<hr>
<h4 id="2-let-与-const"><a href="#2-let-与-const" class="headerlink" title="2.let 与 const"></a>2.let 与 const</h4><p>let 声明的变量只在 let 命令所在的代码块内有效。</p>
<p>const 声明一个只读的常量，一旦声明，常量的值就不能改变。const其实是让这个变量指向的地址数据不能发生变化，当变量是普通类型的时候，就不能变。当变量是数组，对象这些<strong>引用类型</strong>的时候，因为这个地址只是指向实际数据的指针，所以可以改变这些引用类型里面的数据，只是const定义的变量不能改变。</p>
<hr>
<h4 id="3-解构赋值"><a href="#3-解构赋值" class="headerlink" title="3.解构赋值"></a>3.解构赋值</h4><p>解构赋值是对赋值运算符的扩展，他是一种针对数组或者对象进行模式匹配，然后对其中的变量进行赋值。</p>
<p>可进行嵌套解构。</p>
<p>**从对象或数组中提取数据, 并赋值给变量(多个)**。</p>
<p>注意当对对象进行解构的时候，<strong>在{}里面的变量名需要与被解构的对象key名相同</strong>，不然不能被解构。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> obj = &#123;<span class="attr">name</span> : <span class="string">&#x27;kobe&#x27;</span>, <span class="attr">age</span> : <span class="number">39</span>&#125;;</span><br><span class="line"><span class="keyword">let</span> name = obj.name;</span><br><span class="line"><span class="keyword">let</span> age = obj.age;</span><br><span class="line"><span class="comment">//解构</span></span><br><span class="line"><span class="keyword">let</span> &#123;name,age&#125; = obj;</span><br><span class="line"><span class="keyword">let</span> [a,b,c] = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</span><br><span class="line"><span class="built_in">console</span>.log(name,age)</span><br><span class="line"><span class="comment">//注意这种方式，是对常见等号赋值的一种对称形式	source --&gt; target</span></span><br><span class="line"><span class="keyword">let</span> &#123;<span class="attr">name</span>:x,<span class="attr">age</span>:y&#125; = obj;</span><br><span class="line"><span class="built_in">console</span>.log(name,age)  <span class="comment">//referrence error</span></span><br><span class="line"><span class="built_in">console</span>.log(x,y)  <span class="comment">//‘kobe’ 39</span></span><br></pre></td></tr></table></figure>
<hr>
<h4 id="4-展开符-三点运算符-…-rest参数"><a href="#4-展开符-三点运算符-…-rest参数" class="headerlink" title="4.展开符/三点运算符(…)/rest参数"></a>4.展开符/三点运算符(…)/rest参数</h4><p>用来取代 arguments 但比 arguments 灵活.里面的value是一个真正的数组，而不是像argument一样的伪数组</p>
<p>而且只能是函数参数的最后一个位置，即后面不能有其他的参数</p>
<p><img src="https://gitee.com/xxgw1997/Web-2/raw/master/12-ES6/images/9.png" alt="img"></p>
<p>或者可用来对数组进行扩展</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>, <span class="number">6</span>];</span><br><span class="line"><span class="keyword">let</span> arr1 = [<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line">arr = [<span class="number">1</span>, ...arr1, <span class="number">6</span>];</span><br><span class="line">f(...arr1);</span><br><span class="line"><span class="built_in">console</span>.log(arr); <span class="comment">// [1,2,3,4,5,6]</span></span><br><span class="line"><span class="built_in">console</span>.log(...arr); <span class="comment">// 1 2 3 4 5 6</span></span><br></pre></td></tr></table></figure>

<hr>
<h4 id="5-async-函数"><a href="#5-async-函数" class="headerlink" title="5.async 函数"></a>5.async 函数</h4><p>async 函数返回一个 Promise 对象，可以使用 then 方法添加回调函数。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">helloAsync</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">await</span> <span class="string">&quot;helloAsync&quot;</span>;</span><br><span class="line">  &#125;</span><br><span class="line"><span class="built_in">console</span>.log(helloAsync())  <span class="comment">// Promise &#123;&lt;resolved&gt;: &quot;helloAsync&quot;&#125;</span></span><br><span class="line">helloAsync().then(<span class="function"><span class="params">v</span>=&gt;</span>&#123;</span><br><span class="line">   <span class="built_in">console</span>.log(v);         <span class="comment">// helloAsync。async里的return返回会成为then()回调函数的参数。</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>async 函数中可能会有 await 表达式，async 函数执行时，如果遇到 await 就会先暂停执行 ，等到触发的异步操作完成后，恢复 async 函数的执行并返回解析值。</p>
<h4 id="6-class类"><a href="#6-class类" class="headerlink" title="6.class类"></a>6.class类</h4><p>为了方便创建对象。形似java里面的class类</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//定义类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Point</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">x, y</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.x = x;</span><br><span class="line">    <span class="built_in">this</span>.y = y;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="function"><span class="title">toString</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&#x27;(&#x27;</span> + <span class="built_in">this</span>.x + <span class="string">&#x27;, &#x27;</span> + <span class="built_in">this</span>.y + <span class="string">&#x27;)&#x27;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> newclass = <span class="keyword">new</span> Point(x,y);<span class="comment">//创建</span></span><br></pre></td></tr></table></figure>

<p><code>constructor</code>方法是类的默认方法，通过<code>new</code>命令生成对象实例时，自动调用该方法。一个类必须有<code>constructor</code>方法，如果没有显式定义，一个空的<code>constructor</code>方法会被默认添加。</p>
<hr>
<p>class可以继承。<strong>extends</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ColorPoint</span> <span class="keyword">extends</span> <span class="title">Point</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">x, y, color</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">super</span>(x, y); <span class="comment">// 调用父类的constructor(x,y)，子类必须在constructor方法中调用super方法，主要构建父类的this对象。</span></span><br><span class="line">    <span class="built_in">this</span>.color = color;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="function"><span class="title">toString</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.color + <span class="string">&#x27; &#x27;</span> + <span class="built_in">super</span>.toString(); <span class="comment">// 调用父类的toString()</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
<p> <strong>现在对es6进行一个更细致的学习，主要是对阮一峰的es6教程的学习</strong></p>
<img src="https://user-gold-cdn.xitu.io/2020/3/15/170de4be1318de5e?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="ES6ç¼©ç¥" style="zoom: 50%;" />

<h4 id="1-模板字符串"><a href="#1-模板字符串" class="headerlink" title="1.模板字符串"></a>1.模板字符串</h4><p>使用 <strong>``</strong> 这两个反引号字符里面包围的字符串称为模板字符串，使用 <strong>${}</strong> 可使用正常作用域里面可使用的变量，函数</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> x = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">let</span> y = <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="string">`<span class="subst">$&#123;x&#125;</span> + <span class="subst">$&#123;y&#125;</span> = <span class="subst">$&#123;x + y&#125;</span>`</span></span><br><span class="line"><span class="comment">// &quot;1 + 2 = 3&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="string">`<span class="subst">$&#123;x&#125;</span> + <span class="subst">$&#123;y * <span class="number">2</span>&#125;</span> = <span class="subst">$&#123;x + y * <span class="number">2</span>&#125;</span>`</span></span><br><span class="line"><span class="comment">// &quot;1 + 4 = 5&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> obj = &#123;<span class="attr">x</span>: <span class="number">1</span>, <span class="attr">y</span>: <span class="number">2</span>&#125;;</span><br><span class="line"><span class="string">`<span class="subst">$&#123;obj.x + obj.y&#125;</span>`</span></span><br></pre></td></tr></table></figure>

<h4 id="2-尾调用，尾递归"><a href="#2-尾调用，尾递归" class="headerlink" title="2.尾调用，尾递归"></a>2.尾调用，尾递归</h4><p>在函数的<strong>return</strong>后面加一个函数，可以优化调用栈，在严格模式下启用</p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">晟松</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://chengsong.info/2020/07/24/ES6学习/">https://chengsong.info/2020/07/24/ES6学习/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://chengsong.info">晟松</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/es6/">es6</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/07/29/typescript%E5%88%9D%E5%AD%A6%E4%B9%A0/"><i class="fa fa-chevron-left">  </i><span>typescript初学习</span></a></div><div class="next-post pull-right"><a href="/2020/07/23/vant,%E6%8F%92%E4%BB%B6,%E6%B7%B7%E5%85%A5,%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8C%87%E4%BB%A4/"><span>vant,插件,混入,自定义指令</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer class="footer-bg" style="background-image: url(/img/blog-bg.jpg)"><div class="layout" id="footer"><div class="copyright">&copy;2020 - 2023 By 晟松</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="icp"><a target="_blank" rel="noopener" href="http://www.beian.miit.gov.cn/"><span>湘ICP备2020021729号</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>